<template>
    <view class="Login">
        <button class="register" @getuserinfo="userInfoCallBack" open-type="getUserInfo">授权获取用户信息</button>
    </view>
</template>

<script>
import http from '@/utils/http.js';
export default {
    data(){
        return{

        }
    },
    methods:{
        async userInfoCallBack(e){
            console.log(e);
            const {encryptedData, rawData, iv, signature, userInfo}=e.detail;
            //点击授权获取用户信息,如果点击否提示
            if(!encryptedData){
                uni.showToast({
                    title:'用户拒绝授权',
                    icon:'none'
                })
                return
            }
            if(!encryptedData){
                uni.showToast({
                    title:'用户拒绝授权',
                    icon:'none'
                })
                return
            }
            const [err,res]=await uni.login()
            const { code }=res;
            const data = await http.post('/users/wxlogin', { encryptedData, rawData, iv, signature, code });
            //把token保存到本地存储
            uni.setStorage({
                key:'token',
                data:data.token
            })
            uni.setStorage({
                key:'userInfo',
                data: userInfo
            })
            uni.showToast({
                title:'登录成功'
            })
            //回退到上一页
            setTimeout(()=>{
                uni.navigateBack()
            },1000)
        }
    }

}
</script>

<style lang="less" scoped>
    .Login{
        display: flex;
		align-items: center;
		justify-content: center;
		height: 100vh;
        .register{
            display: flex;
            justify-content: center;
            align-items: center;
            width: 461rpx;
            height: 88rpx;
            border-radius: 6rpx;
            color: #fff;
            background-color: #07c160;
            font-weight: bold;
        }
    }
</style>